﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UltiCoreLibrary;

namespace UltiSheet
{
    public partial class Form1 : Form
    {
        private GameSetup gameSetup = new GameSetup();
        private GameState gameState = new GameState();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            gameSetup.GameState = gameState;
            gameSetup.Show();
            gameSetup.Closing += delegate(object s, CancelEventArgs ea)
                {
                    columnHeaderPlayer1.Text = gameState.players[0].Name;
                    columnHeaderPlayer2.Text = gameState.players[1].Name;
                    columnHeaderPlayer3.Text = gameState.players[2].Name;
                };
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            PartySetup partySetup = new PartySetup();
            partySetup.GameState = gameState;
            partySetup.Show();
            partySetup.Closed += delegate(object s, EventArgs ea)
            {
                Contra contra = new Contra();
                contra.GameState = gameState;
                contra.Show();
                contra.Closed += delegate(object s1, EventArgs e2)
                {
                    PartyResult partyResult = new PartyResult();
                    partyResult.GameState = gameState;
                    partyResult.Show();
                    partyResult.Closed += delegate(object t, EventArgs eat)
                    {

                        Party p = gameState.parties.Last.Value;
                        p.Point.Score += p.pointPrize;
                        p.Defender1.Score += p.defender1Prize;
                        p.Defender2.Score += p.defender2Prize;

                        
                        ListViewItem lvi = new ListViewItem(new string[5] { 
                            gameState.players[0].Score.ToString(),
                            gameState.players[1].Score.ToString(),
                            gameState.players[2].Score.ToString(),
                            p.Dealer.Name,
                            p.partyResult});
                        lvi.Tag = p;
                        // listView1.
                        listView1.Items.Add(lvi);
                    };
                };
            };
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            MessageBox.Show("Party information is not available.", "Not implemented", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            System.Xml.XmlDocument document = new System.Xml.XmlDocument();

            System.Xml.XmlElement session = document.CreateElement("session");

            System.Xml.XmlElement players = document.CreateElement("players");

            document.AppendChild(session);
            session.AppendChild(players);

            for (int i = 0; i < 3; i++)
            {
                System.Xml.XmlElement player = document.CreateElement("player");
                System.Xml.XmlElement playerName = document.CreateElement("name");
                playerName.AppendChild(document.CreateTextNode(gameState.players[i].Name));
                
                player.AppendChild(playerName);
                players.AppendChild(player);
            }

            System.Xml.XmlElement parties = document.CreateElement("parties");
            session.AppendChild(parties);

            foreach (Party party in gameState.parties)
            {
                System.Xml.XmlElement partyNode = document.CreateElement("party");

                System.Xml.XmlElement dealer = document.CreateElement("dealer");
                dealer.AppendChild(document.CreateTextNode(party.Dealer.Name));
                partyNode.AppendChild(dealer);

                System.Xml.XmlElement point = document.CreateElement("point");
                point.AppendChild(document.CreateTextNode(party.Point.Name));
                partyNode.AppendChild(point);

                System.Xml.XmlElement games = document.CreateElement("games");
                partyNode.AppendChild(games);

                System.Xml.XmlElement result = document.CreateElement("result");
                result.AppendChild(document.CreateTextNode(party.partyResult));
                partyNode.AppendChild(result);

                System.Xml.XmlElement trump = document.CreateElement("trump");
                trump.AppendChild(document.CreateTextNode(party.Game.Trump.Name));
                partyNode.AppendChild(trump);


                foreach (BasicGame game in party.Game.SubGames.Keys)
                {
                    System.Xml.XmlElement gameNode = document.CreateElement("game");

                    System.Xml.XmlElement gameName = document.CreateElement("name");
                    gameNode.AppendChild(gameName);
                    gameName.AppendChild(document.CreateTextNode(game.Name));
                    System.Xml.XmlElement contras = document.CreateElement("contras");
                    gameName.AppendChild(contras);

                    if (party.Game.Trump.Color == GameColors.COLOR_COLORLESS)
                    {
                        System.Xml.XmlElement contra1 = document.CreateElement("contra");
                        System.Xml.XmlAttribute value1 = document.CreateAttribute("value");
                        value1.Value = party.ContraDefender1.Value.ToString();
                        contra1.Attributes.Append(value1);
                        contra1.AppendChild(document.CreateTextNode(party.Defender1.Name));
                        contras.AppendChild(contra1);

                        System.Xml.XmlElement contra2 = document.CreateElement("contra");
                        System.Xml.XmlAttribute value2 = document.CreateAttribute("value");
                        value2.Value = party.ContraDefender2.Value.ToString();
                        contra2.Attributes.Append(value2);
                        contra2.AppendChild(document.CreateTextNode(party.Defender2.Name));
                        contras.AppendChild(contra2);
                    }
                    else
                    {
                        System.Xml.XmlElement contra1 = document.CreateElement("contra");
                        System.Xml.XmlAttribute value1 = document.CreateAttribute("value");
                        value1.Value = party.Game.SubGames[game].Value.ToString();
                        contra1.Attributes.Append(value1);
                        contra1.AppendChild(document.CreateTextNode(party.Defender1.Name));
                        contras.AppendChild(contra1);

                        System.Xml.XmlElement contra2 = document.CreateElement("contra");
                        System.Xml.XmlAttribute value2 = document.CreateAttribute("value");
                        value2.Value = party.Game.SubGames[game].Value.ToString();
                        contra2.Attributes.Append(value2);
                        contra2.AppendChild(document.CreateTextNode(party.Defender2.Name));
                        contras.AppendChild(contra2);

                    }
                    System.Xml.XmlAttribute won = document.CreateAttribute("won");
                    won.Value = game.Success.ToString();
                }


                // party.Game.


                parties.AppendChild(partyNode);
            }


            document.Save("UltiSheet-" + DateTime.Now.ToFileTime().ToString() + ".xml");
        }

    }
}